/* eslint-disable @typescript-eslint/no-explicit-any */
import { mainProgressNoticeType } from './../renderer/src/type'
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron'
import { electronAPI } from '@electron-toolkit/preload'
import { compressOptions } from '../renderer/src/type'

// Custom APIs for renderer
const api = {
  // 压缩
  compress: (options: compressOptions) => {
    ipcRenderer.invoke('compress', options)
  },
  // 选中文件
  selectDirectory: () => {
    return ipcRenderer.invoke('selectDirectory')
  },
  // 单个进度事件通知
  // progressNotice: (callback: (progress: number) => void) => {
  //   ipcRenderer.on('progressNotice', (_event: IpcRendererEvent, progress: number) => {
  //     callback(progress)
  //   })
  // }
  // 主进程事件通知
  mainProgressNotice: (callback: (type: mainProgressNoticeType, options: any) => void) => {
    ipcRenderer.on(
      'mainProgressNotice',
      (_event: IpcRendererEvent, type: mainProgressNoticeType, options: any) => {
        callback(type, options)
      }
    )
  },
  stop: () => {
    ipcRenderer.send('stop')
  }
}

// Use `contextBridge` APIs to expose Electron APIs to
// renderer only if context isolation is enabled, otherwise
// just add to the DOM global.
if (process.contextIsolated) {
  try {
    contextBridge.exposeInMainWorld('electron', electronAPI)
    contextBridge.exposeInMainWorld('api', api)
  } catch (error) {
    console.error(error)
  }
} else {
  // @ts-ignore (define in dts)
  window.electron = electronAPI
  // @ts-ignore (define in dts)
  window.api = api
}
